﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using Orchard.ImportExport.Services;
using Wng.Deployment.Models;

namespace Wng.Deployment.Handlers {
    public class DeploymentMetadataExportEventHandler : IExportEventHandler {
        public const string StepName = "DeploymentMeta";

        public void Exporting(ExportContext context) {
            //Not required
        }

        public void Exported(ExportContext context) {
            var deploymentMetaSteps = context.ExportOptions.CustomSteps
                .Select(c => DeploymentMetadata.FromExportStep(c)).Where(c => c != null);

            if (!deploymentMetaSteps.Any())
                return;

            var recipeDescription = string.Join(";", deploymentMetaSteps.Select(m => m.ToDisplayString()));
            context.Document.Element("Orchard").Element("Recipe").Add(new XElement("Description", recipeDescription));
        }
    }
}